import com from "./com";
import config from "./config";
import api from "./api";
export default {
	page: 1,
	url: "",
	data: {},
	list: [],
	noMoreData: false,
	cbFunction: undefined,
	animateFunc: undefined,
	lastNotEmptyData: [],
	initialize: function() {
		this.data["page"] = this.page
	},
	getList: function() {
		var _this = this;

		if (_this.noMoreData) {
			com.showToast("没有更多数据了")
			_this.cbFunction(_this.list);
		} else {
			_this.data.page = this.page
			if (_this.animateFunc !== undefined) {
				_this.animateFunc()
			}
			api.get(this.url, this.data, function(res) {
				if (_this.lastNotEmptyData === res.d) {
					return;
				}
				if (res.d.length === 0 && _this.list.length > 0) {
					com.showToast("没有更多数据了");
					_this.noMoreData = true;
					_this.cbFunction(_this.list);
				} else {
					_this.lastNotEmptyData = res.d;
					var orgList = _this.list
					_this.list = orgList.concat(res.d);
					_this.cbFunction(_this.list);
					_this.page++;
				}
			});
		}
		return this;
	},
	reload: function() {
		this.page = 1;
		this.list = [];
		this.noMoreData = false;
		this.getList();
		return this;
	},
	setUrl: function(url) {
		this.url = url
		return this;
	},
	setData: function(data) {
		this.data = data
		this.data.page = this.page
		return this;
	},
	setCbFunc: function(func) {
		this.cbFunction = func
		return this;
	},
	setAnimateFunc: function(func) {
		this.animateFunc = func
		return this;
	}
}
